<%- include("header",{type:'create'}) %>
<div class="container">
    <form style="width:100%" method="post" class="form create">
        <div>
            <label>标题：</label>
            <input placeholder="请输入标题" type="text" name="title">
        </div>
        <div>
            <label>内容：</label>
            <textarea placeholder="请输入内容" name="content" id="" cols="42" rows="10"></textarea>
        </div>
        <div class="submit">发表</div>
    </form>
</div>
<script>
    $('.submit').click(() => {
        if ($('input[name=title]').val().trim() == '') {
            fade('请输入标题')
        } else if ($('textarea').val().trim() == '') {
            fade('请输入内容')
        } else {
            $.ajax({
                url: "/create",
                data: $('.form').serialize(),
                type: "POST",
                cache: false,
                dataType: 'json',
                success: function (msg) {
                    if (msg) {
                        fade('发表成功')
                        setTimeout(() => {
                            window.location.href = "/posts"
                        }, 1000)
                    } else {
                        fade('发表失败')
                    }
                },
                error: function () {
                    alert('异常');
                }
            })
        }
    })
</script>
<% include footer %>